package com.qfjg.controllers;


import com.qfjg.entity.PhoneList;
import com.qfjg.myutil.ResultData;
import com.qfjg.service.PhoneListService;
import com.qfjg.webcore.auth.Pass;
import com.qfjg.webcore.common.Utils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "PhoneList", description = "项目填报手机信息相关")
@RestController
@RequestMapping("/${spring.application.name}/PhoneList")
public class PhoneListController {

    @Autowired
    private PhoneListService phoneListService;

    @PostMapping("/insertData")
    @Operation(summary = "插入数据")
    @Pass
    public ResultData insert(@RequestBody PhoneList phoneList) {

        try {
            if (phoneListService.selectPhoneNumber(phoneList.getProjectId(), phoneList.getPhoneNumber()) != 0)
                return new ResultData<>(340, "出现错误: 已存在手机号", null);
            phoneList.setPhId(Utils.uuid());
            phoneListService.insert(phoneList);
            return ResultData.SUCCESS();
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


    @PostMapping("/deleteData/{phId}")
    @Operation(summary = "通过phId删除数据")
    @Pass
    public ResultData deleteById(@PathVariable String phId) {
        try {
            boolean b = phoneListService.deleteById(phId);
            if (b)
                return ResultData.SUCCESS();
            return new ResultData<>(444, "出现错误: 删除操作失败", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }

    @PostMapping("/selectPhoneList/{projectId}")
    @Operation(summary = "通过projectId查询项目填报手机数据")
    @Pass
    public ResultData selectPhoneList(@PathVariable String projectId) {
        try {
            List<PhoneList> phoneLists = phoneListService.selectDumpRecordList(projectId);
            return new ResultData<>(phoneLists);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultData<>(444, "出现错误: " + e.getMessage(), null);
        }
    }


}
